package com.baibei.ebec.home.common;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.util.ViewUtil;
import com.baibei.model.ShipmentInfo;
import com.blankj.utilcode.utils.ScreenUtils;
import com.blankj.utilcode.utils.TimeUtils;
import com.rae.widget.dialog.impl.SlideDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 首页船信息弹窗
 * 发货人:
 * 收货人:
 * 通知人:
 * 船名/航次:
 * 收货地:
 * 装货港:
 * 卸货港:
 * 目的地:
 * 中转港以及预计到中转港时间:
 * 目的地运输方式:
 * Created by shenminjie on 2017/8/7.
 * mail:shenminjie@100bei.com
 */

public class ShipmentInfoFragment extends DialogFragment {

    private static final String TAG = "tag";

    @BindView(R2.id.tv_ship_title)
    TextView mTvShipTitle;
    @BindView(R2.id.tv_sender)
    TextView mTvSender;
    @BindView(R2.id.tv_receiver)
    TextView mTvReceiver;
    @BindView(R2.id.tv_notification)
    TextView mTvNotification;
    @BindView(R2.id.tv_ship_name)
    TextView mTvShipName;
    @BindView(R2.id.tv_receive_place)
    TextView mTvReceivePlace;
    @BindView(R2.id.tv_port_of_loading)
    TextView mTvPortOfLoading;
    @BindView(R2.id.tv_port_of_discharge)
    TextView mTvPortOfDischarge;
    @BindView(R2.id.tv_target)
    TextView mTvTarget;
    @BindView(R2.id.tv_transport_target)
    TextView mTvTransportTarget;
    @BindView(R2.id.tv_transport_time)
    TextView mTvTransportTime;
    @BindView(R2.id.tv_transport_target_way)
    TextView mTvTransportTargetWay;
    Unbinder unbinder;
    @BindView(R2.id.scrollview)
    NestedScrollView mScrollview;
    @BindView(R2.id.layout_container)
    LinearLayout mLayoutContainer;

    private ShipmentInfo mInfo;

    /**
     * 设置信息
     *
     * @param info info
     */
    public void setInfos(ShipmentInfo info) {
        mInfo = info;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ship_info, container);
        unbinder = ButterKnife.bind(this, view);
        ViewUtil.setViewWidth(mLayoutContainer, ScreenUtils.getScreenWidth());
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //填充数据
        if (mInfo == null) {
            mInfo = new ShipmentInfo();
        }
        //禁止手势
        mScrollview.setNestedScrollingEnabled(false);
        mTvShipTitle.setText(mInfo.getName());
        mTvSender.setText(mInfo.getDeliverUser());
        mTvReceiver.setText(mInfo.getConsignee());
        mTvNotification.setText(mInfo.getNotifier());
        mTvShipName.setText(mInfo.getShipmentNum());
        mTvReceivePlace.setText(mInfo.getDestination());
        mTvPortOfLoading.setText(mInfo.getDepartPort());
        mTvPortOfDischarge.setText(mInfo.getDischargePort());
        mTvTarget.setText(mInfo.getDestination());

        mTvTransportTime.setText(mInfo.getEntrepotTime() == null ? "" : TimeUtils.millis2String(mInfo.getEntrepotTime()));
        mTvTransportTarget.setText(mInfo.getEntrepotPort());
        mTvTransportTargetWay.setText(mInfo.getDestinationTransport());
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new SlideDialog(getContext());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 显示数据
     *
     * @param manager manager
     * @param info    info
     */
    public static void show(FragmentManager manager, ShipmentInfo info) {
        ShipmentInfoFragment shipmentInfoFragment = new ShipmentInfoFragment();
        shipmentInfoFragment.setInfos(info);
        shipmentInfoFragment.show(manager, TAG);
    }

    @OnClick(R2.id.tv_close)
    public void onViewClicked() {
        dismiss();
    }
}
